﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using DMS.BaseClass;
using DMS.Business.Utility;
using DMS.Business.Modules.Organization;
using DMS.Business.Utility;
using System.Text;
using System.Web.UI.HtmlControls;

namespace DMS.Web.Common.Controls
{
    public partial class UserRoleList : ListControlBase
    {
        #region 成员属性
        private bool _AutoDataBind = true;
        private bool _ShowHiddenRole = false;
        private List<string> _SelectedRoles = new List<string>();

        public List<string> SelectedRoles
        {
            get { return _SelectedRoles; }
            set { _SelectedRoles = value; }
        }

        public bool ShowHiddenRole
        {
            get { return _ShowHiddenRole; }
            set { _ShowHiddenRole = value; }
        }

        public bool AutoDataBind
        {
            get { return _AutoDataBind; }
            set { _AutoDataBind = value; }
        }

        public string[] SelectedItem
        {
            get
            {
                return this.hidSelectedItem.Value.Split(";".ToCharArray());
            }
            set
            {
                this.hidSelectedItem.Value = string.Join(";", value);
            }
        }

        private string _OnClientSelectBtnClick = string.Empty;
        public string OnClientSelectBtnClick
        {
            get
            {
                return this._OnClientSelectBtnClick;
            }
            set
            {
                this._OnClientSelectBtnClick = value;
            }
        }
        protected override List<Control> ControlSet
        {
            get
            {
                List<Control> ctrls = new List<Control>();

                return ctrls;
            }
        }
        protected override List<string> ControlSetInGrid
        {
            get
            {
                List<string> ctrlIds = new List<string>();

                return ctrlIds;
            }
        }
        protected override Dictionary<int, string> ColumnStateSettings
        {
            get
            {
                Dictionary<int, string> settings = new Dictionary<int, string>();
                foreach (ListItem item in this.lbGridColState.Items)
                {
                    try
                    {
                        int key = Convert.ToInt32(item.Text);
                        string value = item.Value;
                        settings.Add(key, value);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.LogException(ex);
                    }
                }

                return settings;
            }
        }
        protected override WebControl MainListControl
        {
            get
            {
                return this.dgRoleList;
            }
        }
        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (this._AutoDataBind)
                {
                    this.InitDataList();
                }
            }
        }
        protected void dgRoleList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            {
                return;
            }

            Role role = e.Item.DataItem as Role;
            if (role == null)
            {
                return;
            }

            HtmlAnchor lkSelectBtn = e.Item.FindControl("lkSelectBtn") as HtmlAnchor;
            if (lkSelectBtn != null && !string.IsNullOrEmpty(this._OnClientSelectBtnClick))
            {
                string script = this._OnClientSelectBtnClick;
                if (this._OnClientSelectBtnClick.Contains("%ActorType%"))
                {
                    script = script.Replace("%ActorType%", role.ActorType.ToString());
                }
                if (this._OnClientSelectBtnClick.Contains("%ID%"))
                {
                    script = script.Replace("%ID%", role.ID.ToString());
                }
                if (this._OnClientSelectBtnClick.Contains("%Name%"))
                {
                    script = script.Replace("%Name%", role.Name);
                }
                if (this._OnClientSelectBtnClick.Contains("%Type%"))
                {
                    script = script.Replace("%Type%", role.Type.ToString());
                }
                if (this._OnClientSelectBtnClick.Contains("%IsBuildIn%"))
                {
                    script = script.Replace("%IsBuildIn%", role.IsBuildIn.ToString());
                }
                if (this._OnClientSelectBtnClick.Contains("%Description%"))
                {
                    script = script.Replace("%Description%", role.Description);
                }
                lkSelectBtn.Attributes["onclick"] = string.Format("javascript:{0};", script);
            }
        }

        public void InitDataList()
        {
            int total = 0;
            StringBuilder condition = new StringBuilder();
            if (!this._ShowHiddenRole)
            {
                condition.Append("AND [IsDisplay] = 1");
            }

            List<Role> roles = UserHelper.GetRoles(-1, 1, out total, condition.ToString(), null);
            if (roles.Count > 0)
            {
                this.dgRoleList.DataSource = roles;
                this.DataBind();

                this.dgRoleList.Visible = true;
                this.dvEmpty.Visible = false;
            }
            else
            {
                this.dgRoleList.Visible = false;
                this.dvEmpty.Visible = true;
            }
        }

        public void BindSelectedItem()
        {
            string script = "InitSelector('{0}', '{1}');";
            if (this.Mode == ListModeTypes.SingleSelector)
            {
                ClientHelper.Instance.RenderScript(this.Page, string.Empty, string.Format(script, "radSelector", this.hidSelectedItem.ClientID));
            }
            else if (this.Mode == ListModeTypes.SelectAction)
            {
                ClientHelper.Instance.RenderScript(this.Page, string.Empty, string.Format(script, "chkSelector", this.hidSelectedItem.ClientID));
            }
        }
    }
}